/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmThayDoiQuyDinh.java
 *
 * Created on Aug 27, 2012, 10:18:46 PM
 */
package gui;

import java.util.List;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;
import pojo.*;
import dao.*;
import javax.swing.JOptionPane;

/**
 *
 * Tên: Nguyễn Thị Ngọc Cẩm Lớp: 11HCB MSSV: 1142006
 *
 */
public class frmThayDoiQuyDinh extends javax.swing.JFrame {

    private int _irow;
    private Cuahang _ch;
    private List<Khosanpham> _lksp;

    public frmThayDoiQuyDinh() {
        initComponents();
        this._ch = new Cuahang();
        this._ch.setMacuahang(1);
        this._ch.setTencuahang("Cua hang Tap hoa 1");
        this.jtxtTenCuaHang.setText(this._ch.getTencuahang());

        this._lksp = KhoSanPhamDAO.layDanhSachKhoSanPham(_ch);
        fillJTable();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jPanelThongTinThayDoi = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jtxtLuongNhapToiDa = new javax.swing.JTextField();
        jtxtLuongTonToiThieu = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jbtnCapNhat = new javax.swing.JButton();
        jtxtTenCuaHang = new javax.swing.JTextField();
        jtxtTenSanPham = new javax.swing.JTextField();
        jButton_LamMoi = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTable_ThongTinKho = new javax.swing.JTable();
        jbtnThoat = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane2.setViewportView(jTable2);

        jButton1.setText("jButton1");

        jTextField1.setText("jTextField1");

        jTextField2.setText("jTextField2");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanelThongTinThayDoi.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Thông Tin Thay Đổi", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Times New Roman", 1, 12), new java.awt.Color(0, 0, 102))); // NOI18N
        jPanelThongTinThayDoi.setName(""); // NOI18N

        jLabel2.setText("Tên Cửa Hàng");

        jLabel3.setText("Số Lượng  Nhập Tối Đa");

        jLabel5.setText("Số Lượng Tồn Tối Thiểu");

        jtxtLuongNhapToiDa.setName("jtxtLuongNhapToiDa"); // NOI18N

        jLabel6.setText("Tên Sản Phẩm");

        jbtnCapNhat.setText("Cập Nhật");
        jbtnCapNhat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnCapNhatActionPerformed(evt);
            }
        });

        jtxtTenCuaHang.setEditable(false);

        jtxtTenSanPham.setEditable(false);

        jButton_LamMoi.setText("Làm Mới");
        jButton_LamMoi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_LamMoiActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanelThongTinThayDoiLayout = new javax.swing.GroupLayout(jPanelThongTinThayDoi);
        jPanelThongTinThayDoi.setLayout(jPanelThongTinThayDoiLayout);
        jPanelThongTinThayDoiLayout.setHorizontalGroup(
            jPanelThongTinThayDoiLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelThongTinThayDoiLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelThongTinThayDoiLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel5)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(jPanelThongTinThayDoiLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jtxtLuongTonToiThieu, javax.swing.GroupLayout.DEFAULT_SIZE, 184, Short.MAX_VALUE)
                    .addComponent(jtxtLuongNhapToiDa)
                    .addComponent(jtxtTenCuaHang))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel6)
                .addGap(18, 18, 18)
                .addGroup(jPanelThongTinThayDoiLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelThongTinThayDoiLayout.createSequentialGroup()
                        .addComponent(jButton_LamMoi, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(jbtnCapNhat))
                    .addComponent(jtxtTenSanPham, javax.swing.GroupLayout.PREFERRED_SIZE, 173, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(28, 28, 28))
        );
        jPanelThongTinThayDoiLayout.setVerticalGroup(
            jPanelThongTinThayDoiLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelThongTinThayDoiLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelThongTinThayDoiLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelThongTinThayDoiLayout.createSequentialGroup()
                        .addGroup(jPanelThongTinThayDoiLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel6)
                            .addComponent(jtxtTenSanPham, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(26, 26, 26)
                        .addGroup(jPanelThongTinThayDoiLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jbtnCapNhat, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jButton_LamMoi, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanelThongTinThayDoiLayout.createSequentialGroup()
                        .addGroup(jPanelThongTinThayDoiLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(jtxtTenCuaHang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanelThongTinThayDoiLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jtxtLuongNhapToiDa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanelThongTinThayDoiLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jtxtLuongTonToiThieu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5))))
                .addGap(17, 17, 17))
        );

        jTable_ThongTinKho.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jTable_ThongTinKho.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable_ThongTinKhoMouseClicked(evt);
            }
        });
        jTable_ThongTinKho.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTable_ThongTinKhoKeyPressed(evt);
            }
        });
        jScrollPane3.setViewportView(jTable_ThongTinKho);

        jbtnThoat.setText("Thoát");
        jbtnThoat.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jbtnThoatMouseClicked(evt);
            }
        });

        jLabel8.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel8.setForeground(new java.awt.Color(0, 0, 102));
        jLabel8.setText("THAY ĐỔI QUY ĐỊNH");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jbtnThoat, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(jScrollPane3))
                        .addGroup(layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(jPanelThongTinThayDoi, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addGap(23, 23, 23))
            .addGroup(layout.createSequentialGroup()
                .addGap(233, 233, 233)
                .addComponent(jLabel8)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel8)
                .addGap(18, 18, 18)
                .addComponent(jPanelThongTinThayDoi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jbtnThoat, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(21, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jTable_ThongTinKhoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable_ThongTinKhoMouseClicked
// TODO add your handling code here:
    this._irow = jTable_ThongTinKho.getSelectedRow();
    this.jtxtTenSanPham.setText(this.jTable_ThongTinKho.getValueAt(_irow, 0).toString());
    this.jtxtLuongNhapToiDa.setText(this.jTable_ThongTinKho.getValueAt(_irow, 1).toString());
    this.jtxtLuongTonToiThieu.setText(this.jTable_ThongTinKho.getValueAt(_irow, 2).toString());
}//GEN-LAST:event_jTable_ThongTinKhoMouseClicked

private void jbtnThoatMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jbtnThoatMouseClicked
// TODO add your handling code here:
    this.setVisible(false);
}//GEN-LAST:event_jbtnThoatMouseClicked

private void jButton_LamMoiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_LamMoiActionPerformed
// TODO add your handling code here:
    this._lksp = KhoSanPhamDAO.layDanhSachKhoSanPham(_ch);
    fillJTable();
    this.jtxtTenSanPham.setText("");
    this.jtxtLuongNhapToiDa.setText("");
    this.jtxtLuongTonToiThieu.setText("");
}//GEN-LAST:event_jButton_LamMoiActionPerformed

private void jbtnCapNhatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnCapNhatActionPerformed
// TODO add your handling code here:
    this._lksp.get(this._irow).setSoluongnhaptoida(Integer.valueOf(this.jtxtLuongNhapToiDa.getText()));
    this._lksp.get(this._irow).setSoluongtontoithieu(Integer.valueOf(this.jtxtLuongTonToiThieu.getText()));

    boolean kq = KhoSanPhamDAO.capnhatThongTinKhoSanPham(this._lksp.get(_irow));
    if (kq) {
        JOptionPane.showMessageDialog(this, "Cập nhật thành công !", "Thông báo", JOptionPane.INFORMATION_MESSAGE);
    } else {
        JOptionPane.showMessageDialog(this, "Cập nhật không được !", "Thông báo", JOptionPane.WARNING_MESSAGE);
    }

    this._lksp = KhoSanPhamDAO.layDanhSachKhoSanPham(_ch);
    fillJTable();
    this.jtxtLuongNhapToiDa.setText(this.jTable_ThongTinKho.getModel().getValueAt(_irow, 0).toString());
    this.jtxtLuongTonToiThieu.setText(this.jTable_ThongTinKho.getModel().getValueAt(_irow, 1).toString());
}//GEN-LAST:event_jbtnCapNhatActionPerformed

    private void jTable_ThongTinKhoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTable_ThongTinKhoKeyPressed
        // TODO add your handling code here:
        this._irow = jTable_ThongTinKho.getSelectedRow();
        int key = evt.getKeyCode();
        if (key == 40) {//nhan nut xuong
            this._irow++;
        }
        if (key == 38) {//nhan nutlen
            this._irow--;
        }
        this.jtxtTenSanPham.setText(this.jTable_ThongTinKho.getValueAt(_irow, 0).toString());
        this.jtxtLuongNhapToiDa.setText(this.jTable_ThongTinKho.getValueAt(_irow, 1).toString());
        this.jtxtLuongTonToiThieu.setText(this.jTable_ThongTinKho.getValueAt(_irow, 2).toString());
    }//GEN-LAST:event_jTable_ThongTinKhoKeyPressed

    private void fillJTable() {
        DefaultTableModel tableModel = new DefaultTableModel();
        tableModel.addColumn("Tên Sản Phẩm");
        tableModel.addColumn("Số lượng nhập tối đa");
        tableModel.addColumn("Số lượng nhập tối thiểu");
        tableModel.addColumn("Số lượng tồn kho");
        for (int i = 0; i < this._lksp.size(); i++) {
            Vector vt = new Vector();
            vt.add(this._lksp.get(i).getSanpham().getTensanpham());
            vt.add(this._lksp.get(i).getSoluongnhaptoida());
            vt.add(this._lksp.get(i).getSoluongtontoithieu());
            vt.add(this._lksp.get(i).getSoluongton());

            tableModel.addRow(vt);
        }
        this.jTable_ThongTinKho.setModel(tableModel);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(frmThayDoiQuyDinh.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(frmThayDoiQuyDinh.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(frmThayDoiQuyDinh.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(frmThayDoiQuyDinh.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new frmThayDoiQuyDinh().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton_LamMoi;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanelThongTinThayDoi;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable jTable2;
    private javax.swing.JTable jTable_ThongTinKho;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JButton jbtnCapNhat;
    private javax.swing.JButton jbtnThoat;
    private javax.swing.JTextField jtxtLuongNhapToiDa;
    private javax.swing.JTextField jtxtLuongTonToiThieu;
    private javax.swing.JTextField jtxtTenCuaHang;
    private javax.swing.JTextField jtxtTenSanPham;
    // End of variables declaration//GEN-END:variables
}
